package cn.px.rest.controller.store;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.store.TransferStoreFormDTO;
import cn.px.dao.entity.store.TransferStoreForm;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.store.ExportTransferStoreFormVO;
import cn.px.dao.vo.query.store.TransferStoreFormVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.store.TransferStoreFormService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 出库登记单 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/store/transfer-store-forms")
public class TransferStoreFormController {

  private final  TransferStoreFormService outStoreFormService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<TransferStoreFormVO> getById(@PathVariable("id") String id){
      return new R<>(outStoreFormService.getDetail(id));
  }

  /**
   * 新增记录
   *
   * @param outStoreFormDTO 出库登记单DTO
   * @return R
   */
  @SysLog("新增出库登记单")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('store-out-store-forms-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody TransferStoreFormDTO outStoreFormDTO) {
      return new R<>(outStoreFormService.saveModelByDTO(outStoreFormDTO));
  }

  /**
   * 修改记录
   *
   * @param outStoreFormDTO 出库登记单DTO
   * @return R
   */
  @SysLog("修改出库登记单")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('store-out-store-forms-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody TransferStoreFormDTO outStoreFormDTO) {
      outStoreFormService.updateModelByDTO(outStoreFormDTO);
      return new R<>();
  }

    /**
     * 提交出库
     *
     * @param id 出库登记单id
     * @return R
     */
    @SysLog("修改出库登记单")
    @PutMapping("/out/{id}")
    @PreAuthorize("@pms.hasPermission('store-out-store-forms-edit')")
    public R update(@PathVariable("id") String id) {
        outStoreFormService.updateStore(id);
        return new R<>();
    }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除出库登记单")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('store-out-store-forms-del')")
  public R removeById(@PathVariable String id){
    outStoreFormService.cascadingDeleteByKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除出库登记单")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('store-out-store-forms-del')")
    public R removeIds(@RequestBody List<String> ids){
        outStoreFormService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param outStoreFormDTO 出库登记单DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<TransferStoreFormVO>> query(TransferStoreFormDTO outStoreFormDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(outStoreFormService.selectPageList(outStoreFormDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param outStoreFormDTO 出库登记单DTO
   * @return R
   */
  @GetMapping("")
  public R query(TransferStoreFormDTO outStoreFormDTO) {
      return new R<>(outStoreFormService.getModelListByDTO(outStoreFormDTO));
  }

  /**
     * 出库登记单导出
     *
     * @param outStoreFormDTO 出库登记单DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("出库登记单导出")
  @PreAuthorize("@pms.hasPermission('store-out-store-forms-export')")
  public void export(HttpServletResponse response, TransferStoreFormDTO outStoreFormDTO) {
    List<TransferStoreForm> list = outStoreFormService.getModelListByDTO(outStoreFormDTO);
    ExcelUtil.exportResponseDict(response, ExportTransferStoreFormVO.class, BeanConverterUtil.copyListProperties(list, ExportTransferStoreFormVO.class), "出库登记单");
  }

}
